package util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IndividualConverter {

	private static int maxInt = 10;
	private static int minInt = 0;

	private static int MAX_FACTOR = 1000000;

	public static int convertToInt(double individual) {

		return (int) ((individual / (maxInt - minInt)) * MAX_FACTOR);

	}
	
	public static List<Integer> convertToInt(List<Double> list){
		
		List<Integer> result = new ArrayList<Integer>();
		
		for(Double value: list){
			result.add(convertToInt(value));
		}
		return result;
	}

	public static double convertToDouble(int individual) {

		return ((double) individual) / MAX_FACTOR * (maxInt - minInt);

	}

	public static List<Double> convertToDouble(List<Integer> list){

		List<Double> result = new ArrayList<Double>();

		for(Integer value: list){
			result.add(convertToDouble(value));
		}
		return result;
	}

	public static String toBinaryString(Integer individual) {

		/*
		 * 
		 */
		String resp = Integer.toBinaryString(individual);
		int respL = resp.length();
		for( int i = respL; i < 32;i++){
			resp = "0"+resp;
		}
		return resp;
		/*
		char sign = individual > 0 ? '0' : '1';

		String ret = Integer.toBinaryString(individual);

		if (ret.length() != Integer.SIZE) {

			char[] trail = new char[Integer.SIZE - ret.length()];
			Arrays.fill(trail, sign);

			ret = String.valueOf(trail) + ret;

		}

		return ret;
		*/
	}
	
	public static String toBinaryString2(Integer individual) {

		/*
		 * 
		 */
//		String resp = Integer.toBinaryString(individual);
//		int respL = resp.length();
//		for( int i = respL; i < 32;i++){
//			resp = "0"+resp;
//		}
//		return resp;
		
		char sign = individual >= 0 ? '0' : '1';

		String ret = Integer.toBinaryString(individual);

		if (ret.length() != Integer.SIZE) {

			char[] trail = new char[Integer.SIZE - ret.length()];
			Arrays.fill(trail, sign);

			ret = String.valueOf(trail) + ret;

		}

		return ret;
		
	}	
	

	public static int fromBinaryString(String binstr) {

		char sign = binstr.charAt(0);
		if (sign != '0') {
			if (sign == '1') {
				sign = '-';
			} else {
				throw new IllegalArgumentException(
						"el parámetro tiene que ser la representación ");
			}
		}

		return 0; // TODO cambiar
	}

}
